<?php
include_once '../Exceptions/CommunicationException.php';
include_once '../Exceptions/MuchParametersException.php';
include_once '../InfoManager/InfoManager.php';
include_once '../Model/Location.php';
include_once '../Model/StepRoute.php';
include_once '../AtacCommunicator/ACRoutePlanner.php';
include_once '../Utility/GoogleGeocode.php';
include_once '../Utility/BaseGeocode.php';


/**
 * @author Laura Magrini
 * 
 */
 
if(isset($_GET['end'])){ //miglioramento dell'indirizzo di destinazione (@author Matteo Nati)
  
 	$geo = new GoogleGeocode(""); //step 1: lo chiedo a google; se mi restiuisce un indirizzo valido lo salvo
	$coord = $geo->geocode($_GET['end'].", Roma, Italia");
	if(!empty($coord['Placemarks'][0])){
		if(!empty($coord['Placemarks'][0]['Thoroughfare'])){
			$_GET['end'] = $coord['Placemarks'][0]['Thoroughfare'];
		}
		else if(!empty($coord['Placemarks'][0]['Latitude']) && !empty($coord['Placemarks'][0]['Longitude'])){ //step 2: se google restituisce solo delle coordinate, ricavo l'indirizzo
			$url= "http://maps.google.com/maps/geo?q=".$coord['Placemarks'][0]['Latitude'].",".$coord['Placemarks'][0]['Longitude']."&output=kml";
	  	$xml = simplexml_load_file($url);
			$ns = $xml->getDocNamespaces();
			if(isset($ns[""])){
				$xml->registerXPathNamespace("default",$ns[""]);
			}
			$pois= $xml->xpath('//default:Placemark');
			if(!empty($pois)){
				$values = array("Via","Piazza","Vicolo","Viale","Largo","Piazzale","Corso","Galleria","Strada","Porta","Rione","Stradone");
				$chunks = explode(",",(string)$pois[0]->address);
				$found=0;
				foreach($chunks as $c)
					foreach($values as $v)
						if(stripos(trim($c),$v)!==false){
							$_GET['end']=trim($c);
							$found=1;
						}
				if($found==0)
					$_GET['end'] = trim($chunks[0]);
			}
	 	}
	}	
}

if(isset($_GET['start'])){ //miglioramento dell'indirizzo di partenza (@author Matteo Nati)
  
  $geo = new GoogleGeocode("");
	$coord = $geo->geocode($_GET['start'].", Roma, Italia");
	if(!empty($coord['Placemarks'][0]['Thoroughfare'])){ //se google trova un indirizzo lo salvo e elimino le coordinate (se esistono)
		$_GET['start'] = $coord['Placemarks'][0]['Thoroughfare'];
		unset($_GET["lat"]);
		unset($_GET["lat"]);
	}
}

if ((isset($_GET["start"])) && (isset($_GET["end"])) && (!isset($_GET["lat"])) && (!isset($_GET["long"])))
{
	$start = new Location(RMobUtils::escape_var($_GET["start"],null,null));
	$end = new Location(RMobUtils::escape_var($_GET["end"],null,null));
	
}else if ((isset($_GET["lat"])) && (isset($_GET["long"])) && (isset($_GET["end"])) && (!isset($_GET["start"]))){

	$lat = floatval(RMobUtils::escape_var($_GET["lat"]));
	$long = floatval(RMobUtils::escape_var($_GET["long"]));
	$test = new GPS($lat,$long);
	
	$start=new Location(null,null,$test);		
	$end = new Location(RMobUtils::escape_var($_GET["end"],null,null));		
	
}else {
	return;
}

	//mezzo=0 : mezzo privato
	//mezzo=1 : trasporto pubblico
	//mezzo=2 : Escludi metro, ferrovie e mezzo privato
   //mezzo=3 : Trasporto pubblico e bike sharing
   //mezzo=4 : Mezzo privato e trasporto pubblico

if (isset($_GET["mezzo"]))
 	$mezzo = RMobUtils::escape_var($_GET["mezzo"]);
else $mezzo=1;
		try {
			$date=date('d m y H i'); //data attuale es: 9 12 10 11 51
		    $parts = explode(" ",$date);
			$step = new StepRoute($start, $end, $parts[0], $parts[1], $parts[2], $parts[3], $parts[4], $mezzo);
			echo $step->getRouteFromStart($step);
			
		}
		catch (Exception $e) {
			echo $e->toXML(EXTENDED_MODE);
		}
		
?>